package com.kujie.service;

import com.kujie.constant.SmartControlConstant;
import com.kujie.domain.query.ControlAirConditionerQuery;
import com.kujie.domain.query.ControlLightingQuery;
import com.kujie.utils.HttpUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springaicommunity.mcp.annotation.McpTool;
import org.springaicommunity.mcp.annotation.McpToolParam;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 设备信息/控制
 *
 * @author check
 * @date 2025-07-10
 */
@Service
public class DeviceService {
    private static final Logger log = LoggerFactory.getLogger(DeviceService.class);

    @McpTool(name = "get_air_condition_list", description = "获取空调设备列表")
    public String getAirConditionList() throws Exception {
        log.info("获取空调设备列表工具调用");
        ResponseEntity<String> response = HttpUtils.get(SmartControlConstant.AIR_TOTAL_API_URL, null, null, new ParameterizedTypeReference<String>() {
        });
        return response.getBody();
    }

    @McpTool(name = "control_air_condition", description = "控制空调设备状态时，则必须先调用（获取空调设备列表）工具获取对应设备的devId， status为状态：0-关闭，1-打开， 形成json格式参数{{devIdList: [设备id1,设备id2,设备id3, ..... ], status: {status}}}，再次调用（空调设备控制）工具")
    public String officeControlAir(@McpToolParam(description = "状态") Integer status, @ToolParam(description = "设备id列表") List<Integer> devIdList) {
        log.info("控制空调设备状态工具调用");
        ControlAirConditionerQuery query = new ControlAirConditionerQuery(null, null, null, devIdList, status);
        ResponseEntity<String> response = HttpUtils.post(SmartControlConstant.AIR_CONTROL_API_URL, null, query, new ParameterizedTypeReference<String>() {
        });
        return response.getBody();
    }

    @McpTool(name = "get_lighting_list", description = "获取照明/灯光设备列表信息")
    public String getLightingList() throws Exception {
        log.info("获取照明/灯光设备列表工具调用");
        ResponseEntity<String> response = HttpUtils.get(SmartControlConstant.LIGHTING_TOTAL_API_URL, null, null, new ParameterizedTypeReference<String>() {
        });
        return response.getBody();
    }

    @McpTool(name = "control_lighting", description = "控制照明/灯光设备状态时，则必须先调用（获取照明/灯光设备列表信息）工具获取对应设备的devId，传入的参数为: devIdList为devId的集合列表， status为状态：0-关闭，1-打开， 组装成json格式参数{{devIdList: [设备id1,设备id2,设备id3, ..... ], status: {status}}})")
    public String officeControlLighting(@McpToolParam(description = "状态") Integer status, @McpToolParam(description = "设备id列表") List<Integer> devIdList) {
        log.info("控制照明/灯光设备状态工具调用");
        ControlLightingQuery query = new ControlLightingQuery(devIdList, status);
        ResponseEntity<String> response1 = HttpUtils.post(SmartControlConstant.LIGHTING_CONTROL_API_URL, null, query, new ParameterizedTypeReference<String>() {
        });
        return response1.getBody();
    }

    @McpTool(name = "get_office_toilet_usage", description = "获取办公室卫生间使用情况，能够获取到卫生间相关信息，无参数输入")
    public String getOfficeToiletUsage() throws Exception {
        log.info("获取办公室卫生间使用情况工具调用");
        ResponseEntity<String> response = HttpUtils.get(SmartControlConstant.OFFICE_TOILET_USAGE_API_URL, null, null, new ParameterizedTypeReference<String>() {
        });
        return response.getBody();
    }
}
